#!/bin/bash
SCRIPT_BASE_DIR=$( dirname "${BASH_SOURCE[0]}" )
source "${SCRIPT_BASE_DIR}/common/fs_utils.sh"

cmd=$( basename "$0" )

function usage() {
	echo "Usage: ${cmd} [ -h ]"
    echo -e "\t-h: 显示帮助信息"
	exit 1
}
# 默认索引当前目录下所有文件
srcDirs=(. ../../open_source)

# 默认不索引test目录下的文件
findTestOpt="-name test -type d -prune -o"
while getopts "h" args
do
    case "${args}" in 
        *)
            usage;;
    esac
done

for d in "${srcDirs[@]}"
do
    dir_exists_or_die "$d"
done

csFile="./cscope.files"
echo "Build ${csFile} ..."
# 重置文件列表
echo > "${csFile}"

# 建造文件列表
for d in "${srcDirs[@]}"
do
    find "$d/" ${findTestOpt} -name "*.c" -type f -print >> "${csFile}"
    find "$d/" ${findTestOpt} -name "*.cpp" -type f -print >> "${csFile}"
    find "$d/" ${findTestOpt} -name "*.cc" -type f -print >> "${csFile}"
    find "$d/" ${findTestOpt} -name "*.h" -type f -print >> "${csFile}"
    find "$d/" ${findTestOpt} -name "*.hpp" -type f -print >> "${csFile}"
done

# 建立cscope数据库
echo "Build cscope DB..."
cscope -Rbqk
echo "Done."
